package offer.offer02;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class S27二叉树的镜像II {
    public TreeNode mirrorTree(TreeNode root) {
        List<TreeNode> nodeList = new ArrayList<>();
        nodeList.add(root);
        int[] valueList = makeList(nodeList, new int[] {});
        TreeNode result = makeReverseTree(valueList);
        return result;
    }

    public int[] makeList(List<TreeNode> nodeList, int[] initList){
        List<Integer> result = new ArrayList<>();

        int nodeListCount = 0;
        while(nodeListCount < nodeList.size()){
            TreeNode tmpNode = nodeList.get(nodeListCount);
            if(tmpNode.left != null) nodeList.add(tmpNode.left);
            if(tmpNode.right != null) nodeList.add(tmpNode.right);
            result.add(tmpNode.val);
            System.out.println(tmpNode.val);
            nodeListCount++;
        }

        int[] result2 = new int[result.size()];
        int count = 0;
        for(Integer integer: result){
            result2[count] = integer;
            count ++ ;
        }
        return result2;
    }

    public TreeNode makeReverseTree(int[] inputList){
        List<TreeNode> result = new ArrayList<>();

        int nodeListCount = 0;
        while(nodeListCount < inputList.length){
            TreeNode tmpNode = new TreeNode(inputList[nodeListCount]);
            result.add(tmpNode);
            if(nodeListCount != 0){
                if(nodeListCount %2 == 1){
                    System.out.println(result.get((nodeListCount-1)/2).val +
                            "的右子树: " + tmpNode.val);
                    result.get((nodeListCount-1)/2).right = tmpNode;
                }else{
                    System.out.println(result.get((nodeListCount-1)/2).val +
                            "的左子树: " + tmpNode.val);
                    result.get((nodeListCount-1)/2).left = tmpNode;
                }
            }
            nodeListCount ++;
        }

        return result.get(0);
    }

    public void printTree(TreeNode initNode){
        if(initNode != null){
            System.out.println(initNode.val);
            printTree(initNode.left);
            printTree(initNode.right);
        }

    }

//    public static void main(String[] args) {
//
//        TreeNode a = new TreeNode(1);
//        a.left = new TreeNode(2);
//        a.right = new TreeNode(3);
//        a.left.left = new TreeNode(4);
//        a.left.right = new TreeNode(5);
//        a.right.left = new TreeNode(6);
//
//        List<TreeNode> nodeList = new ArrayList<>();
//        nodeList.add(a);
//        Solution27 template = new Solution27();
//        int[] valueList = template.makeList(nodeList, new int[] {});
//        System.out.println("*************");
//        for(int i: valueList) System.out.println(i);
//
//        System.out.println("*************");
//        TreeNode reversedTree = template.makeReverseTree(valueList);
//        System.out.println("******打印最终结果******");
//        template.printTree(reversedTree);
//
//    }
}